# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Konstantin Podsvirov <konstantin@podsvirov.pro>

_realname=freeglut
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.4.0
pkgrel=2
pkgdesc="Freeglut allows the user to create and manage windows containing OpenGL contexts (mingw32-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://freeglut.sourceforge.io/"
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
options=('strip' '!libtool' 'staticlibs')
source=("https://github.com/FreeGLUTProject/freeglut/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "003-freeglut-3.2.1-install-glut-h.patch"
        "004-pkg-config.patch")
sha256sums=('3c0bcb915d9b180a97edaebd011b7a1de54583a838644dcd42bb0ea0c6f3eaec'
            '0d091fba73641bdc51d28763101452f606c26aed30c9ac1682a19ba9306de8aa'
            'c867bf702a2507e28139659f5c0fa54c83588aa7402a45c5031d3f15d8918148')
noextract=(${_realname}-${pkgver}.tar.gz)

prepare() {
  tar -xzvf ${_realname}-${pkgver}.tar.gz -C ${srcdir}

  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/003-freeglut-3.2.1-install-glut-h.patch
  patch -p1 -i "${srcdir}"/004-pkg-config.patch
}

build() {
  [[ -d "build-${MSYSTEM}" ]] && rm -rf "build-${MSYSTEM}"
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    ${_extra_config[@]} \
    ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE
}
